package com.dailyblue.project.estate.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.project.estate.commons.bean.ChargeDetail;
import com.dailyblue.project.estate.commons.handler.ChargeDetailException;
import com.dailyblue.project.estate.commons.mapper.ChargeDetailMapper;
import com.dailyblue.project.estate.commons.util.DateUtil;
import com.dailyblue.project.estate.commons.util.JsonResult;
import com.dailyblue.project.estate.commons.util.ResultTool;
import com.dailyblue.project.estate.service.ChargeService;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther:Liu
 * @Testname:ChargeServiceImpl
 * @Date:2025/9/18 13:57
 */
@Service
public class ChargeServiceImpl extends ServiceImpl<ChargeDetailMapper, ChargeDetail> implements ChargeService {
    @Resource
    private ChargeDetailMapper chargeDetailMapper;
    @Override
    public JsonResult saveChargeDetail(ChargeDetail chargeDetail) {
        boolean b = save(chargeDetail);
        if (!b) {
            throw new ChargeDetailException("添加费用明细失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult findOneById(String id) {
        ChargeDetail chargeDetail = getById(id);
        return ResultTool.success(chargeDetail);
    }

    @Cacheable(cacheNames = "ChargeDetails")
    public JsonResult findAll() {
        List<ChargeDetail> list = list();
        return ResultTool.success(list);
    }

    @CacheEvict(cacheNames = "ChargeDetails", allEntries = true)
    public JsonResult deleteOneById(String id) {
        boolean b = removeById(id);
        if (!b) {
            throw new ChargeDetailException("删除费用明细失败");
        }
        return ResultTool.success("删除费用明细成功");
    }

    @CacheEvict(cacheNames = "ChargeDetails", allEntries = true)
    public JsonResult updateOneById(ChargeDetail chargeDetail) {
        UpdateWrapper<ChargeDetail> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", chargeDetail.getId());
        wrapper.set("comunity_id", chargeDetail.getCommunityId());
        wrapper.set("community_name", chargeDetail.getCommunityName());
        wrapper.set("charge_item_id", chargeDetail.getChargeItemId());
        wrapper.set("charge_item_name", chargeDetail.getChargeItemName());
        wrapper.set("contractor", chargeDetail.getContractor());
        wrapper.set("telephone", chargeDetail.getTelephone());
        wrapper.set("payMoney", chargeDetail.getPayMoney());
        wrapper.set("actualMoney", chargeDetail.getActualMoney());
        wrapper.set("payTime", DateUtil.nowDate());
        wrapper.set("status", chargeDetail.getStatus());
        int update = chargeDetailMapper.update(wrapper);
        if (update != 1) {
            throw new ChargeDetailException("更新费用明细失败");
        }
        return ResultTool.success("更新费用明细成功");
    }
}
